using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 减速Buff - 降低目标移动速度
/// </summary>
public class SlowBuff : BuffBase
{
    private float m_OriginalSpeed;
    private float m_SlowPercentage;
    private float m_NewSpeed;

    public SlowBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
        Logger.Log("SlowBuff", $"SlowBuff 构造函数被调用，target={target?.name}, buffData类型={buffData?.GetType().Name}");
        
        if (buffData is SlowData slowData)
        {
            m_SlowPercentage = slowData.SlowPercentage;
            Logger.Log("SlowBuff", $"从 SlowData 读取减速百分比: {m_SlowPercentage * 100}%, target={target?.name}");
        }
        else
        {
            m_SlowPercentage = 0.5f; // 默认减速50%
            Logger.LogWarning("SlowBuff", $"BuffData 不是 SlowData 类型，使用默认减速百分比: {m_SlowPercentage * 100}%, target={target?.name}, buffData类型={buffData?.GetType().Name}");
        }
    }

    protected override void OnStart()
    {
        Logger.Log("SlowBuff", $"OnStart 被调用，target={Target?.name}, slowPercentage={m_SlowPercentage * 100}%");
        base.OnStart();
        
        // 保存原始速度
        if (Target.MoveComponent != null)
        {
            m_OriginalSpeed = Target.MoveComponent.Speed;
            m_NewSpeed = m_OriginalSpeed * (1.0f - m_SlowPercentage);
           
            Logger.Log("SlowBuff", $"应用减速: 原始速度={m_OriginalSpeed}, 新速度={m_NewSpeed}, 减速百分比={m_SlowPercentage * 100}%, target={Target.name}");
            Target.MoveComponent.SetSpeed(m_NewSpeed);
            
            // 验证速度是否设置成功
            float verifySpeed = Target.MoveComponent.MoveSpeed;
            if (Mathf.Abs(verifySpeed - m_NewSpeed) > 0.001f)
            {
                Logger.LogError("SlowBuff", $"速度设置失败！期望={m_NewSpeed}, 实际={verifySpeed}, target={Target.name}");
            }
            else
            {
                Logger.Log("SlowBuff", $"速度设置成功，验证通过: {verifySpeed}, target={Target.name}");
            }
        }
        else
        {
            Logger.LogWarning("SlowBuff", $"Target 没有 MoveComponent，无法应用减速效果！target={Target?.name}");
        }
        
        // 播放减速特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
            Logger.Log("SlowBuff", $"播放减速特效，target={Target.name}");
        }
        else
        {
            Logger.Log("SlowBuff", $"BuffData.Effect 为空，不播放特效，target={Target.name}");
        }
        
        // 设置动画速度
        if (Target.Animator != null)
        {
            float speedMultiplier = 1.0f - m_SlowPercentage;
            Target.Animator.SetFloat("speedMultiplier", speedMultiplier);
            Logger.Log("SlowBuff", $"设置动画速度倍数: {speedMultiplier}, target={Target.name}");
        }
        else
        {
            Logger.Log("SlowBuff", $"Target 没有 Animator，不设置动画速度，target={Target.name}");
        }
        
        // 受到减速攻击时变蓝，使用BuffData的Duration作为颜色持续时间
        float colorDuration = BuffData != null ? BuffData.Duration : 0.2f;
        Target.SetColor(Color.blue, colorDuration);
        
        Logger.Log("SlowBuff", $"{Target.name} 被减速了，移动速度降低 {m_SlowPercentage * 100}%");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        // 减速期间不需要特殊处理
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        Logger.Log("SlowBuff", $"{Target.name} 减速效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        // 恢复原始速度
        if (Target.MoveComponent != null)
        {
            Logger.Log("SlowBuff", $"恢复基础速度，target={Target.name}, BaseSpeed={Target.MoveComponent.BaseSpeed}");
            Target.MoveComponent.ResetToBaseSpeed();
            Logger.Log("SlowBuff", $"速度已重置，当前速度={Target.MoveComponent.MoveSpeed}, target={Target.name}");
        }
        
        // 恢复动画速度
        if (Target.Animator != null)
        {
            Target.Animator.SetFloat("speedMultiplier", 1.0f);
        }
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("Slow"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return true; // 减速效果可以叠加，刷新持续时间
    }
}
